export const uniqueId = 'x'

export function copyText(text: string) {
  if (navigator.clipboard) {
    return navigator.clipboard.writeText(text)
  }
  if (document) {
    const textarea = document.createElement('textarea')
    textarea.textContent = text
    document.body.appendChild(textarea)
    textarea.select()
    document.execCommand('copy')
    textarea.blur()
    document.body.removeChild(textarea)
    return Promise.resolve()
  }
  return Promise.reject(new Error(`navigator.clipboard 和 document 皆不可用`))
}

export function registerScripting(key: string, callback: () => void) {
  if (Reflect.get(window, key)) return

  Reflect.defineProperty(window, key, {
    enumerable: true,
    value: true
  })

  callback()
}

export function typeString<T>(target: any): target is T {
  return Object.prototype.toString.call(target) as any
}
